package com.yyt.module_main.vm;

import android.databinding.ObservableField;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.vondear.rxtool.RxRegTool;
import com.yyt.libsrc.bean.BaseCallbackBean;
import com.yyt.libsrc.conf.Consts;
import com.yyt.libsrc.helper.MsgHelper;
import com.yyt.libsrc.net.JavaParamsBuilder;
import com.yyt.libsrc.net.NetUtil;
import com.yyt.libsrc.net.SimpleNetCallBack;
import com.yyt.libsrc.net.SuccessCallback;
import com.yyt.libsrc.router.RouteTable;
import com.yyt.libsrc.router.RouterHelper;

import top.lixb.libcommon.binding.BindingViewModel;
import top.lixb.libcommon.dialog.IOSAlertDialog;
import top.lixb.libcommon.utils.CountDownUtil;

public class ForgetPwdVM extends BindingViewModel {
    public ObservableField<String> mobile = new ObservableField<>();
    public ObservableField<String> smscode = new ObservableField<>();
    public ObservableField<String> pwd1 = new ObservableField<>();
    public ObservableField<String> pwd2 = new ObservableField<>();


    /**
     * 忘记密码
     * @param view
     */
    public void commitUpdatePwd(View view) {
        if (!RxRegTool.isMobileSimple(mobile.get())) {
            MsgHelper.showToast("手机号码不正确");
            return;
        }
        if (!TextUtils.isEmpty(pwd1.get()) && !TextUtils.equals(pwd1.get(), pwd2.get())) {
            MsgHelper.showToast("两次输入的密码不一致");
            return;
        }
        NetUtil.sellerPost(new JavaParamsBuilder()
                .addCmd(Consts.CMD.updatePwd)
                .addP("phone",mobile.get())
                .addP("authCode",smscode.get())
                .addP("loginPwd",pwd1.get())
                .build(), new SuccessCallback<BaseCallbackBean>() {
            @Override
            public void onSuccess(BaseCallbackBean baseCallbackBean) {
                if (null!=mActivity) {
                    new IOSAlertDialog(mActivity).builder().setMsg("修改密码成功，是否前往登录").setPositiveButton("确定", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            MsgHelper.showToast("更新密码成功");
                            Bundle bundle = new Bundle();
                            bundle.putString("mobile", mobile.get());
                            bundle.putString("pwd", pwd1.get());
                            RouterHelper.getInstance().startAct(RouteTable.main_login,bundle);
                            finish();
                        }
                    }).show();
                }
            }
        });
    }

    /**
     * 清空输入
     * @param view
     */
    public void clearInput(View view) {
        mobile.set("");
    }

}
